﻿using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Xml.Serialization;
using Eflong.Utility;

namespace Eflong.Config.DataFilter
{
    public class DataFilterConfigurationManager
    {
        private static readonly string ConfigName = "DataFilter";
        private static readonly string _configFile;
        private static readonly ManualResetEvent InitSingle;
        private static readonly object SyncRoot;

        static DataFilterConfigurationManager( )
        {
            _configFile = KWBridgeConfig.Instance.DataFilterConfig;

            if ( string.IsNullOrEmpty( _configFile ) )
                _configFile = Path.Combine( AppDomain.CurrentDomain.BaseDirectory , ConfigName + ".config" );

            if ( !File.Exists( _configFile ) )
                throw new Exception( "没有找到限值配置文件：" + ConfigName + ".config" );

            SyncRoot = new object( );
            FileWatcher.Instance.AddFile( _configFile , OnConfigChanged );
            InitSingle = new ManualResetEvent( false );
            InitConfig( );
        }

        private static void OnConfigChanged( object sender , EventArgs args )
        {
            InitConfig( );
        }

        private static DataFilterConfiguration _config;

        public static DataFilterConfiguration Config
        {
            get
            {
                return _config;
            }
            set
            {
                _config = value;
            }
        }

        /// <summary>
        /// 提交配置更改
        /// </summary>
        public static void SubmitChanges( )
        {
            lock ( SyncRoot )
            {
                InitSingle.Reset( );
                try
                {
                    Save( );
                }
                finally
                {
                    InitSingle.Set( );
                }
            }
        }

        private static void Save( )
        {
            using ( FileStream fs = new FileStream( _configFile , FileMode.Create , FileAccess.Write ) )
            {
                XmlSerializer ser = new XmlSerializer( typeof( DataFilterConfiguration ) , new Type[ ]　{　
　　　　　　　
　　　　　　} );
                ser.Serialize( fs , _config );
            }
        }

        /// <summary>
        /// 初始化配置文件
        /// </summary>
        private static void InitConfig( )
        {
            lock ( SyncRoot )
            {
                InitSingle.Reset( );
                try
                {
                    Load( );
                }
                finally
                {
                    InitSingle.Set( );
                }
            }
        }

        /// <summary>
        /// 加载配置文件
        /// </summary>
        private static void Load( )
        {
            if ( !File.Exists( _configFile ) )
            {
                _config = new DataFilterConfiguration( );
                return;
            }
            using ( FileStream fs = new FileStream( _configFile , FileMode.Open , FileAccess.Read ) )
            {
                XmlSerializer ser = new XmlSerializer( typeof( DataFilterConfiguration ) , new Type[ ]　{　
　　　　　　　　
　　　　　　} );
                _config = ( DataFilterConfiguration )ser.Deserialize( fs );
            }
        }
    }
}